<?php

namespace backend\models\c2\form;

use common\models\c2\statics\AttributeInputType;
use yii\validators\Validator;

/**
 * Created by Adu at 2020/4/7
 */
class EavForm extends \yii\base\Model
{
    use \kzo\base\models\ModelTrait;

    private $_attributes = [];
    private $_value = [];
    protected $_eavAttributes = [];
    protected $_eavAttributeLabels = [];
    protected $_extraParams = [];
    public $entityModel = null;
    public $message;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        if (is_null($this->entityModel)) {
            throw new \yii\base\Exception('Entity is required!');
        }
        $this->_value = $this->entityModel->getEavValues();
        $eavAttrs = $this->getEntityEavAttributes();
        foreach ($eavAttrs as $eavCode => $eavAttr) {
            if ($eavAttr->is_required) {
                $this->addRule([$eavCode], 'required');
            }
            if ($eavAttr->is_unique) {
                $this->addRule([$eavCode],
                    \common\components\validators\EavUniqueValidator::className(), [
                        'entityValModel' => $this->entityModel->getVModel($eavCode),
                        'targetClass' => $this->entityModel->getVModelClass($eavCode),
                        'targetAttribute' => 'value'
                    ]);
            }
            if (!$eavAttr->is_required && !$eavAttr->is_unique) {
                $this->addRule([$eavCode], 'safe');
            }
        }
    }

    public function addRule($attributes, $validator, $options = [])
    {
        $validators = $this->getValidators();
        $validators->append(Validator::createValidator($validator, $this, (array)$attributes, $options));
        return $this;
    }

    public function getEntityEavAttributes()
    {
        if (empty($this->_eavAttributes)) {
            $metaAttributes = $this->entityModel->getNonSkuMetaAttributes()->all();
            foreach ($metaAttributes as $attribute) {
                $this->_eavAttributes[$attribute->code] = $attribute;
            }
        }
        return $this->_eavAttributes;
    }

    public function getEavFormAttributes()
    {
        $eavAttrs = [];
        $attributes = $this->getEntityEavAttributes();
        foreach ($attributes as $attribute) {
            switch ($attribute->input_type) {
                case \common\models\c2\statics\AttributeInputType::INPUT_DROPDOWN_LIST:
                case \common\models\c2\statics\AttributeInputType::INPUT_CHECKBOX_LIST:
                case \common\models\c2\statics\AttributeInputType::INPUT_RADIO_LIST:
                    $eavAttrs[$attribute->code] = [
                        'type' => $attribute->input_type,
                        'hint' => $attribute->hint,
                        'items' => $attribute->getItemsHashMap('id', 'label')
                    ];
                    break;
                case AttributeInputType::INPUT_RICHTEXT:
                    $eavAttrs[$attribute->code] = [
                        'type' => AttributeInputType::INPUT_WIDGET,
                        'widgetClass' => '\vova07\imperavi\Widget',
                        'options' => [
                            'settings' => [
                                'minHeight' => 150,
                                'buttonSource' => true,
                                'lang' => \Yii::$app->kzoHelper->getRegularLangName(),
                                'plugins' => [
                                    'fontsize',
                                    'fontfamily',
                                    'fontcolor',
                                    'table',
                                    'textdirection',
                                    'fullscreen',
                                ],
                            ]
                        ],
                    ];
                    break;
                case AttributeInputType::INPUT_TEXT:
                case AttributeInputType::INPUT_TEXTAREA:
                default:
                    $eavAttrs[$attribute->code] = [
                        'type' => $attribute->input_type,
                        'hint' => $attribute->hint,
                        'options' => [
                            'placeholder' => $this->getAttributeLabel($attribute->code)
                        ]
                    ];
                    break;
            }
        }
        return $eavAttrs;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->_value)) {
            if (!empty($this->_value[$name])) {
                return $this->_value[$name];
            } else {
                $this->getEntityEavAttributeDefault($name);
            }
        }
        return parent::__get($name); // TODO: Change the autogenerated stub
    }

    public function getEntityEavAttributeDefault($name)
    {
        $attr = $this->getEntityEavAttribute($name);
        if (!is_null($attr)) {
            return $attr->getEavDefaultValue();
        }
        return null;
    }

    public function getEntityEavAttribute($code)
    {
        $attrs = $this->getEntityEavAttributes();
        return isset($attrs[$code]) ? $attrs[$code] : null;
    }

}